package com.yunduo.aspect;

import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.RetunData.statuscode.impl.ServerCode;
import com.yunduo.common.RetunData.statuscode.impl.UserCode;
import com.yunduo.entity.vo.LoginUser;
import com.yunduo.schedule.TimerTask.UserOutLoginTask;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class UserAspect {
    @Resource
    private RedisTemplate redisTemplate;

    //今日登录人数添加,当前在线人数添加(与token过期时间一致 一小时)
    @AfterReturning(value = "execution(* com.yunduo.controller.AuthController.login(..))",returning = "result")
    public void loginUserTotal(JoinPoint point, Object result) {
        LoginUser user = (LoginUser)point.getArgs()[0];
        CommonReturn commonReturn = (CommonReturn)result;
        String message = commonReturn.getMessage();
        //是否登录成功
        if(UserCode.USER_LOGIN_SUCCESS.getInfo().equals(message)){
            SetOperations setOperations = redisTemplate.opsForSet();
            setOperations.add(ServerCode.TODAY_LOGIN_ACCOUNT_TOAL.getInfo(),user.getName());
            setOperations.add(ServerCode.ONLINE_ACCOUNT_TOTAL.getInfo(),user.getName());
            Timer timer = new Timer();
        }
    }

}
